﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.HVAC;
using YRevitJig;

namespace YArchitech.Plumbing
{
	public class DrawMultiPipeJig : PELineJig
	{
		public List<double> PipeIntervals { get; set; }

		public UIDocument UIDoc { get; set; }

		public DrawMultiPipeJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.PipeIntervals = new List<double>();
			this.UIDoc = uiDoc;
			base.ResetPoint = XUiJigBase.EResetPoint.eByAxis;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty && paintEventArgs != null)
			{
				try
				{
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					XYZ xyz2 = null;
					bool flag = false;
					if (this.PointsList.Count > 0)
					{
						xyz2 = new XYZ(xyz.X, xyz.Y, this.PointsList[0].Z);
						XYZ axisPoint = DrawMultiPipeUtility.GetAxisPoint(this.UIDoc, xyz2, this.PointsList[this.PointsList.Count - 1]);
						if (axisPoint != null)
						{
							xyz2 = axisPoint;
						}
						this.PointsList.Add(xyz2);
						flag = true;
					}
					this.PipeLines = DrawMultiPipeUtility.DrawMultiLines(this.PointsList, null, xyz2, this.PipeIntervals, false);
					foreach (List<LineInfo> list in this.PipeLines)
					{
						foreach (LineInfo lineInfo in list)
						{
							new XLine2D(base.Revit2Client(lineInfo.StartPoint), base.Revit2Client(lineInfo.EndPoint)).Draw(g);
						}
					}
					if (flag)
					{
						this.PointsList.Remove(xyz2);
					}
				}
				catch (Exception ex)
				{
					ex.ToString();
					throw ex;
				}
			}
		}

		protected List<Pipe> sortPipes(List<Pipe> originList, XYZ currentPoint)
		{
			List<Pipe> result = null;
			if (originList.Count > 0)
			{
				result = (from a in originList
				orderby ((a.Location as LocationCurve).Curve as Line).Distance(currentPoint) descending
				select a).ToList<Pipe>();
			}
			return result;
		}

		public List<XYZ> PointsList = new List<XYZ>();

		public List<List<LineInfo>> PipeLines = new List<List<LineInfo>>();
	}
}
